<script type="text/javascript">
$(document).ready (function () {
	$(".fullsearch .query").keyup (function () {
		$(".fullsearch .choices").children ().each (function (i) {
			$(this).detach ();
			$(".fullsearch .master").append ($(this));
		});
		var newval = $(this).val ().toLowerCase ();
		if (newval == "")
			$(".fullsearch .master").children ().each (function (i) {
				$(this).detach ();
				$(".fullsearch .choices").append ($(this));
			});
		else $(".fullsearch .master").children ().each (function (i) {
			if ($(this).text ().toLowerCase ().indexOf (newval) != -1) {
				$(this).detach ();
				$(".fullsearch .choices").append ($(this));
			}
		});
	});
	function evaluate_times () {
		$(".fullsearch input.min").val ("Sat, 01 Jun 1863 00:00:00 GMT");
		$(".fullsearch input.max").val ("Sat, 01 Jun 2162 00:00:00 GMT");
		active_datasets = $(".fullsearch .selection option");
		active_datasets.each (function (i) {
			dataset_id = $(this).val ();
			oldminstr = $(".fullsearch input.min").val ();
			oldmaxstr = $(".fullsearch input.max").val ();
			mindatestr = ranges[dataset_id][0];
			maxdatestr = ranges[dataset_id][1];
			old_min_date = new Date (oldminstr);
			old_max_date = new Date (oldmaxstr);
			min_date = new Date (mindatestr);
			max_date = new Date (maxdatestr);
			try {
			if (min_date.getTime() > old_min_date.getTime()) {
				$(".fullsearch input.min").val (min_date);
				$(".fullsearch span.min").text (min_date.getFullYear() + "-" + min_date.getMonth() + "-" + min_date.getDay());
			}
			if (max_date.getTime() < old_max_date.getTime()) {
				$(".fullsearch input.max").val (max_date);
				$(".fullsearch span.max").text (max_date.getFullYear() + "-" + max_date.getMonth() + "-" + max_date.getDay());
			}
			} catch (e) {
				alert ("An error occurred: " + e.message);
				return false;
			}
		});
	}
	$(".fullsearch .add").click (function () {
		newElements = $(".fullsearch .choices option:selected");
		newElements.each (function (i) {
			$(this).detach ();
			$(".fullsearch .selection").append ($(this));
		});
		evaluate_times ();
		return false;
	});
	$(".fullsearch .remove").click (function () {
		toRemove = $(".fullsearch .selection option:selected");
		toRemove.each (function (i) {
			$(this).detach ();
			$(".fullsearch .choices").append ($(this));
		});
		evaluate_times ();
		return false;
	});
	$(".fullsearch .create").click (function () {
		$(".fullsearch .selection option").each (function (i) {
			$(this).attr("selected","selected");
		});
	});

	date_options = {changeMonth:true,changeYear:true,dateFormat:"yy-mm-dd"};
	$(".fullsearch .startdate").datepicker (date_options);
	$(".fullsearch .enddate").datepicker (date_options);
	$(".fullsearch .query").keyup ();

	ranges = {
	{% for dataset in datasets %}
		"{{ dataset.key }}" : ["{{ dataset.js_start }}", "{{ dataset.js_end }}"],
	{% endfor %}
	};
	
	{% if datasets_used %}
	{% for dataset in datasets_used %}
	opt = $(".fullsearch .choices option[value='{{ dataset.key }}']");
	opt.detach ();
	$(".fullsearch .selection").append (opt);
	{% endfor %}
	evaluate_times ();
	{% endif %}
});
</script>
